﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq.SqlClient;

public partial class UserControls_DatePicker : System.Web.UI.UserControl
{
    #region Properties
    DateTime _SelectedDate = DateTime.MinValue;
    public DateTime SelectedDate
    {
        get
        {
            _SelectedDate = DateTime.MinValue;
            //Assuming actual date value will start from Index = 1
            if (ddlDate.SelectedIndex > 0 && ddlMonth.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                _SelectedDate = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDate.SelectedValue));
            }
            return _SelectedDate;
        }
        set
        {
            ;
        }
    }

    int _DateDiffInYear = 0;
    public int DateDiffInYear
    {
        get
        {
            _DateDiffInYear = 0;
            if (SelectedDate != null)
            {
                TimeSpan tsAge = DateTime.Today.Subtract(SelectedDate);
                _DateDiffInYear = SqlMethods.DateDiffYear(SelectedDate, DateTime.Today);
            }
            return _DateDiffInYear;
        }
    }

    bool _IsValidDate = false;
    public bool IsValidDate
    {
        get
        {
            _IsValidDate = false;

            if (ddlDate.SelectedIndex <= 0 || ddlMonth.SelectedIndex <= 0 || ddlYear.SelectedIndex <= 0)
            {
                return false;
            }

            if (ddlDate.SelectedIndex > 0 && ddlMonth.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                try
                {
                    DateTime dtDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDate.SelectedValue));
                    _IsValidDate = true;
                }
                catch
                {
                    _IsValidDate = false;
                }
            }

            return _IsValidDate;
        }
    } 
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateYearDropDown(ref ddlYear);
            PopulateMonthDropDown(ref ddlMonth);
            PopulateDayDropDown(ref ddlDate);
        }
    }

    protected void DOBDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblAgeValidationMsg.Visible = false;
        if (IsValidDate)
        {
            //Assuming actual date value will start from Index = 1
            if (ddlDate.SelectedIndex > 0 && ddlMonth.SelectedIndex > 0 && ddlYear.SelectedIndex > 0)
            {
                DateTime dtDOB = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDate.SelectedValue));
                if (dtDOB != null)
                {
                    TimeSpan tsAge = DateTime.Today.Subtract(dtDOB);
                    lblAge.Text = "Age: " + SqlMethods.DateDiffYear(dtDOB, DateTime.Today).ToString() + " yrs.";
                }
            }
            else if (ddlDate.SelectedIndex <= 0 && ddlMonth.SelectedIndex <= 0 && ddlYear.SelectedIndex <= 0)
            {
                lblAge.Text = string.Empty;
            }
        }
        else
        {
            lblAgeValidationMsg.Visible = true;
            lblAge.Text = string.Empty;
        }

        (sender as DropDownList).Focus();
    }
    
    private void PopulateYearDropDown(ref DropDownList ddlYear)
    {
        ListItem liYear = new ListItem();
        liYear.Text = "YYYY";
        liYear.Value = "0000";
        ddlYear.Items.Add(liYear);
        int currentYear = DateTime.Now.Year;
        for (int i = 1900; i <= currentYear; i++)
        {
            liYear = new ListItem();
            liYear.Text = i.ToString();
            liYear.Value = i.ToString();
            ddlYear.Items.Add(liYear);
        }
    }

    private void PopulateMonthDropDown(ref DropDownList ddlMonth)
    {
        ListItem liMonth = new ListItem();
        liMonth.Text = "MM";
        liMonth.Value = "00";
        ddlMonth.Items.Add(liMonth);
        string monthText;
        for (int i = 1; i <= 12; i++)
        {
            if (i < 10)
                monthText = "0" + i.ToString();
            else
                monthText = i.ToString();
            liMonth = new ListItem();
            liMonth.Text = monthText.ToString();
            liMonth.Value = monthText.ToString();
            ddlMonth.Items.Add(liMonth);
        }
    }

    private void PopulateDayDropDown(ref DropDownList ddlDay)
    {
        ListItem liDay = new ListItem();
        liDay.Text = "DD";
        liDay.Value = "00";
        ddlDay.Items.Add(liDay);
        string dayText;
        for (int i = 1; i <= 31; i++)
        {
            if (i < 10)
                dayText = "0" + i.ToString();
            else
                dayText = i.ToString();
            liDay = new ListItem();
            liDay.Text = dayText.ToString();
            liDay.Value = dayText.ToString();
            ddlDay.Items.Add(liDay);
        }
    }
       

}